package tcpserver

import (
	"context"
	"fmt"
	"net"
	"runtime"
)

type contextKey struct{
	name string
}

type conn struct{
	server *TCPServer
	cancelCTX context.CancelFunc
	rwc net.Conn
	remoteAddr string
}
type tcpKeepAliveListener struct{
	*net.TCPListener
}

func (c *conn) close(){
	c.rwc.Close()
}

func (c *conn) serve (ctx context.Context){
	defer func() {
		if err := recover(); err != nil && err != ErrAbortHandler{
			const size = 64 << 10
			buf := make([]byte, size)
			buf = buf[:runtime.Stack(buf, false)]
			fmt.Printf("tcp: panic serving %v: %v\n%s", c.remoteAddr, err, buf)
		}
		c.close()
	}()
	c.remoteAddr = c.rwc.RemoteAddr().String()
	ctx = context.WithValue(ctx, LocalAddrContextKey, c.rwc.LocalAddr())
	if c.server.Handler == nil{
		panic("handler empty")
	}
	c.server.Handler.ServeTCP(ctx, c.rwc)
}